<?php

/**
 * Created by xtbb.
 * User: Hoolan
 * Date: 2018-12-4
 * Time: 10:55:32
 */

namespace app\api\controller;
use think\Controller;
use think\Db;

class Login extends Controller
{

    /**
     * 登录验证
     *
     * 描述：验证账号密码是否允许登录
     * 接收参数：userName userPwd code
     * 接收方式：post
     * 返回结果：rt
     */
    public function login()
    {

////声明CODE，获取小程序传过来的CODE
//        $code = $_GET["code"];
////配置appid
//        $appid = "wx5c255aca06378475";
////配置appscret
//        $secret = "ae6699a74ef7ddcd7fa548e4dbb75781";
////api接口
//        $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
////获取GET请求
//        function httpGet($url){
//            $curl = curl_init();
//            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//            curl_setopt($curl, CURLOPT_TIMEOUT, 500);
//            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
//            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
//            curl_setopt($curl, CURLOPT_URL, $url);
//            $res = curl_exec($curl);
//            curl_close($curl);
//            return $res;
//        }
////发送
//        $str = httpGet($api);
//        echo $str;
//
//        $code = $_GET['code'];//获取code
//        $appid ="wx5c255aca06378475";
//        $secret = "ae6699a74ef7ddcd7fa548e4dbb75781";
//        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
//        //通过code换取网页授权access_token
//        $weixin =  file_get_contents($url);
//        echo $weixin;
//        die();
//        $jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
//        $array = get_object_vars($jsondecode);//转换成数组
//
//        $openid = $array['openid'];//输出openid
//
//        return $openid;

        $APPID = 'wx5c255aca06378475';
        $AppSecret = 'ae6699a74ef7ddcd7fa548e4dbb75781';
        $code = input('get.code');

        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$APPID.'&secret='.$AppSecret.'&js_code='.$code.'&grant_type=authorization_code';
        $arr = $this -> vegt($url);

        $arr = json_decode($arr,true);

        if(empty( $arr['openid']) || !isset($arr['openid']) || $arr['openid']=='' || $arr['openid']==null)
        {
            $rt['code']=-10000;
            //设置信息
            $rt['massage']='未获取到微信信息！';
            echo json_encode($rt);
            die();
        }
        $openid = $arr['openid'];


        //结果集rt
        $rt=array();

        //接收用户名
        $username=input('userName');
        //接收密码
        $password=input('userPwd');
        //查询条件
        $where=array(
           array('username','=',$username),
           array('password','=',md5(md5($password))),
        );
        //查询
        $res=Db::table('xt_user')
            ->where($where)
            ->where('delete_time',null)
            ->find();
        //判断查询结果，设置返回信息
        if ($res){
            //设置状态码
            if ($res['openid']){
                if ($openid == $res['openid']){
                    //设置状态码
                    $rt['code']=1;
                    //设置信息
                    $rt['massage']='登录成功！';
                    $rt['status']=true;

                }else{
                    //设置状态码
                    $rt['code']=-10002;
                    //设置信息
                    $rt['massage']='该账号已绑定！';
                    $rt['status']=false;
                }
            }else{
                Db::name('xt_user')->where('id',$res['id'])->setField('openid', $openid);
                //设置状态码
                $rt['code']=1;
                //设置信息
                $rt['massage']='登录成功！';
                $rt['status']=true;
            }
        }else{
            //设置状态码
            $rt['code']=-10001;
            //设置信息
            $rt['massage']='用户名或密码错误！';
            $rt['status']=false;
        }
        //返回结果集
        echo json_encode($rt);

    }
    public function vegt($url){
        $info = curl_init();
        curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($info,CURLOPT_HEADER,0);
        curl_setopt($info,CURLOPT_NOBODY,0);
        curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($info,CURLOPT_URL,$url);
        $output= curl_exec($info);
        curl_close($info);
        return $output;
    }
    public function check(){
        //$openid=$_GET['openid'];
        $res=\think\Db::name('user')->find();
        dump($res);
    }

    public function loginStatus()
    {
        if (session('status')){
            return retJson(true,'1','你开心就好');
        }else{
            return retJson(false,'-1','你开心就好');
        }

    }

}
